package io.renren.utils;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.entity.ColumnEntity;
import io.renren.entity.CustReqDataForBJCRM;
import io.renren.entity.DefectDataForBJCRMDTO;
import io.renren.entity.TaskBean;

/***
 * 通过java Class 创建 建表语句
 */
public class EntityToMysql {
    public static Map<String, String> javaProperty2SqlColumnMap = new HashMap<>();

    static {
        javaProperty2SqlColumnMap.put("Integer", "INTEGER");
        javaProperty2SqlColumnMap.put("int", "INTEGER");
        javaProperty2SqlColumnMap.put("Short", "tinyint");
        javaProperty2SqlColumnMap.put("Long", "bigint");
        javaProperty2SqlColumnMap.put("BigDecimal", "decimal(19,2)");
        javaProperty2SqlColumnMap.put("Double", "double precision not null");
        javaProperty2SqlColumnMap.put("Float", "float");
        javaProperty2SqlColumnMap.put("Boolean", "bit");
        javaProperty2SqlColumnMap.put("Timestamp", "datetime");
        javaProperty2SqlColumnMap.put("Date", "datetime");
        javaProperty2SqlColumnMap.put("String", "VARCHAR(255)");
    }

    public static void main(String[] args) throws IOException {
    	createTable(TaskBean.class, null);
    }
    
    public static String handleColumnString(String columnName) {
    	List<Integer> result=CommonStringHandleUtil.checkStringContainUppercase(columnName);
    	StringBuffer sbu=new StringBuffer();
    	if(!(result==null||result.isEmpty())) {
    		for (int i=0;i<result.size();i++) {
    			Integer cut=result.get(i);
    			Integer nextCut=0;
    			if(i!=result.size()-1) {
    			nextCut=result.get(i+1);
    			}else {
    			nextCut=columnName.length();	
    			}
    			String tmp=new String();
    			String firstPartStr=(i==0)?columnName.substring(0, cut):"";
    			tmp =firstPartStr+"_" + columnName.substring(cut, cut+1).toLowerCase()+columnName.substring(cut+1, nextCut);
    			sbu.append(tmp);
    			
    		}
    		return sbu.toString();
    	}else {
    		return columnName;
    	}
    	
    	 
    	
    }
    


    public static String createTable(Class obj, String tableName) throws IOException {


        Field[] fields = null;
        fields = obj.getDeclaredFields();
        //Class annotationType = null;
        String param = null;
        String column = null;
        String underScoreCaseColumn = null;
        //XmlElement xmlElement = null;
        StringBuilder sb = null;
        sb = new StringBuilder(50);
        if (tableName == null || tableName.equals("")) {
        	//未传表明默认用类名
            tableName = obj.getName();
            tableName = tableName.substring(tableName.lastIndexOf(".") + 1);
        }
        //sb.append("\r\ndrop table if exists  ").append(tableName).append(";\r\n");
        sb.append("create table ").append(tableName).append(" ( \r\n");
       
        boolean firstId = true;
        File file = null;
        for (Field f : fields) {
            column = f.getName();
            if (column.equals("serialVersionUID")) {
                continue;
            }
//            sb.append(column).append(" ");
//            System.out.println(column + "," + f.getType());
            param = f.getType().getSimpleName();
            //sb.append(column);//一般第一个是主键     
            sb.append(handleColumnString(column));
            sb.append(" ").append(javaProperty2SqlColumnMap.get(param)).append(" ");
            
            if (firstId) {//类型转换
                sb.append(" PRIMARY KEY ");
                firstId = false;
            }
            //获取字段中包含fieldMeta的注解
            //2、获取属性上的所有注释
            Annotation[] allAnnotations = f.getAnnotations();
            /*for(Annotation an : allAnnotations){
                sb.append(" COMMIT '");
                xmlElement = (XmlElement)an;
                annotationType = an.annotationType();
                param = ((XmlElement) an).name();
                System.out.println("属性【"+f.getName()+"-----的注释类型有: " + param);
                sb.append(param).append("'");
            }*/
            sb.append(",\n ");
        }
        String sql = null;
        sql = sb.toString();
        //去掉最后一个逗号
        int lastIndex = sql.lastIndexOf(",");
        sql = sql.substring(0, lastIndex) + sql.substring(lastIndex + 1);

        sql = sql.substring(0, sql.length() - 1) + " )ENGINE =INNODB DEFAULT  CHARSET= utf8;\r\n";
        System.out.println("sql :" + sql);
       
        return sql;

    }
}
